package com.leyou.order.web;

import com.leyou.order.service.OrderService;
import com.leyou.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("notify")
public class NotifyController {

    @Autowired
    private OrderService orderService;

    /**
     * 微信支付的成功回调
     * <p>
     * 返回结果 转换为 xml 格式
     *
     * @return
     */
    @PostMapping(value = "pay", produces = MediaType.APPLICATION_XML_VALUE)
    public Map<String, String> notifyMessage(@RequestBody Map<String, String> result) {
        // 处理回调
        orderService.handleNotify(result);

        log.info("[微信支付回调] 接收微信支付回调， 接收的参数：{}", JsonUtils.tostring(result));

        Map<String, String> msg = new HashMap<>();
        // 返回成功
        msg.put("return_code", "SUCCESS");
        msg.put("return_msg", "OK");
        return msg;
    }
}
